package org.jeecg.modules.arc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.arc.entity.ArcAccount;
import org.jeecg.modules.arc.service.IArcAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

 /**
 * @Description: 缴费账户
 * @Author: jeecg-boot
 * @Date:   2022-02-18
 * @Version: V1.0
 */
@Api(tags="缴费账户")
@RestController
@RequestMapping("/arc/arcAccount")
@Slf4j
public class ArcAccountController extends JeecgController<ArcAccount, IArcAccountService> {
	@Autowired
	private IArcAccountService arcAccountService;

	/**
	 * 分页列表查询
	 *
	 * @param arcAccount
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "缴费账户-分页列表查询")
	@ApiOperation(value="缴费账户-分页列表查询", notes="缴费账户-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ArcAccount arcAccount,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		arcAccount.setDelFlag(0);
		//QueryWrapper<ArcAccount> queryWrapper = QueryGenerator.initQueryWrapper(arcAccount, req.getParameterMap());
		Page<ArcAccount> page = new Page<ArcAccount>(pageNo, pageSize);
		IPage<ArcAccount> pageList = arcAccountService.pageList(page, arcAccount);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param arcAccount
	 * @return
	 */
	@AutoLog(value = "缴费账户-添加")
	@ApiOperation(value="缴费账户-添加", notes="缴费账户-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ArcAccount arcAccount) {
		arcAccountService.save(arcAccount);
		return Result.OK("添加成功！");
	}


	 /**
	  *   解除绑定公众号
	  *
	  * @param arcAccount
	  * @return
	  */
	 @AutoLog(value = "解除绑定公众号")
	 @ApiOperation(value="解除绑定公众号", notes="解除绑定公众号")
	 @PostMapping(value = "/cancelBind")
	 public Result<?> cancelBind(@RequestBody ArcAccount arcAccount) {
		 arcAccountService.update(new LambdaUpdateWrapper<ArcAccount>()
				 .set(ArcAccount::getWeixinId,null)
				 .eq(ArcAccount::getId,arcAccount.getId()));
		 return Result.OK("解绑成功！");
	 }

	 /**
	  *   微信通知-停水
	  *
	  * @param arcAccount
	  * @return
	  */
	 @AutoLog(value = "欠费记录-微信欠费催缴")
	 @ApiOperation(value="欠费记录-添加", notes="欠费记录-微信欠费催缴")
	 @GetMapping(value = "/wxmsgStop")
	 public Result<?> wxmsgStop(ArcAccount arcAccount ,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		 return arcAccountService.wxMsgStop(arcAccount);
	 }

	/**
	 *  编辑
	 *
	 * @param arcAccount
	 * @return
	 */
	@AutoLog(value = "缴费账户-编辑")
	@ApiOperation(value="缴费账户-编辑", notes="缴费账户-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ArcAccount arcAccount) {
		arcAccountService.updateById(arcAccount);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "缴费账户-通过id删除")
	@ApiOperation(value="缴费账户-通过id删除", notes="缴费账户-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		ArcAccount arcAccount = arcAccountService.getById(id);
		if(arcAccount==null) {
			return Result.error("未找到对应数据");
		}
		arcAccount.setDelFlag(1);
		arcAccountService.updateById(arcAccount);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "缴费账户-批量删除")
	@ApiOperation(value="缴费账户-批量删除", notes="缴费账户-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		boolean update = arcAccountService.update(new LambdaUpdateWrapper<ArcAccount>()
				.set(ArcAccount::getDelFlag, 1)
				.in(ArcAccount::getId, Arrays.asList(ids.split(","))));
		if(update){
			return Result.OK("批量删除成功!");
		}
		return Result.OK("批量删除异常!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "缴费账户-通过id查询")
	@ApiOperation(value="缴费账户-通过id查询", notes="缴费账户-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ArcAccount arcAccount = arcAccountService.getById(id);
		if(arcAccount==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(arcAccount);
	}
	/**
	 * 通过code查询
	 *
	 * @param accCode
	 * @return
	 */
	@AutoLog(value = "缴费账户-通过id查询")
	@ApiOperation(value="缴费账户-通过id查询", notes="缴费账户-通过id查询")
	@GetMapping(value = "/queryByCode")
	public Result<?> queryByCode(@RequestParam(name="accCode",required=true) String accCode) {
		ArcAccount arcAccount = arcAccountService.getOne(new QueryWrapper<ArcAccount>()
				.lambda().eq(ArcAccount::getCode,accCode).eq(ArcAccount::getDelFlag,0));
		if(arcAccount==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(arcAccount);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param arcAccount
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ArcAccount arcAccount) {
		arcAccount.setDelFlag(0);
        return super.exportXls(request, arcAccount, ArcAccount.class, "缴费账户");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ArcAccount.class);
    }

}
